﻿using System.Globalization;
using Microsoft.SharePoint;
using Microsoft.SharePoint.Workflow;

namespace SKN
{
    /// <summary>
    /// Contains common method for Webpart Workflow
    /// </summary>
    public static class Workflow
    {
        /// <summary>
        /// 
        /// </summary>
        /// <param name="web"></param>
        /// <param name="listName"></param>
        /// <param name="workflowName"></param>
        /// <param name="taskListName"></param>
        /// <param name="workflowHistoryListName"></param>
        /// <param name="manualPermEditListItemRequired"></param>
        /// <param name="manualPermManageListRequired"></param>
        public static void AddWorkflowAssociation(SPWeb web, string listName, string workflowName, string taskListName,
                                                  string workflowHistoryListName, bool manualPermEditListItemRequired,
                                                  bool manualPermManageListRequired)
        {
            //Create a new task list
            web.Lists.Add(taskListName, "This is task list.", SPListTemplateType.Tasks);
            SPList taskList = web.Lists[taskListName];

            //Create a new workflow history list
            web.Lists.Add(workflowHistoryListName, "This is workflow history list.", SPListTemplateType.WorkflowHistory);
            SPList histList = web.Lists[workflowHistoryListName];

            SPWorkflowTemplate wfTemplate = web.WorkflowTemplates.GetTemplateByName(workflowName,
                                                                                    CultureInfo.CurrentCulture);
            SPWorkflowAssociation wfAssociation = SPWorkflowAssociation.CreateListAssociation(wfTemplate,
                                                                                              "Reviewer Alert Workflow",
                                                                                              taskList, histList);

            //Get the list on which workflow will fire
            SPList list = web.Lists[listName];

            //Set workflow options.  
            wfAssociation.AllowManual = true;
            wfAssociation.AutoStartCreate = true;
            wfAssociation.AutoStartChange = false;

            //Permission for Manual 
            if (wfAssociation.AllowManual)
            {
                SPBasePermissions newPerms = SPBasePermissions.EmptyMask;

                if (manualPermEditListItemRequired)
                    newPerms |= SPBasePermissions.EditListItems;
                if (manualPermManageListRequired)
                    newPerms |= SPBasePermissions.ManageLists;

                wfAssociation.PermissionsManual = newPerms;
            }

            //Add workflow association.  
            list.AddWorkflowAssociation(wfAssociation);


            list.Update();
            web.Update();
        }

        /// <summary>
        /// 
        /// </summary>
        /// <param name="web"></param>
        /// <param name="listName"></param>
        /// <param name="workflowName"></param>
        public static void RemoveWorkflowAssociation(SPWeb web, string listName, string workflowName)
        {
            SPList list = web.Lists[listName];

            SPWorkflowAssociation wfAssociation = list.WorkflowAssociations.GetAssociationByName(workflowName,
                                                                                                 CultureInfo.
                                                                                                     CurrentCulture);

            //Remove workflow association.  
            list.RemoveWorkflowAssociation(wfAssociation);

            list.Update();
            web.Update();
        }

        /// <summary>
        /// 
        /// </summary>
        /// <param name="web"></param>
        /// <param name="workflowName"></param>
        /// <param name="listName"></param>
        /// <param name="itemID"></param>
        public static void StartWorkflow(SPWeb web, string workflowName, string listName, int itemID)
        {
            SPList list = web.Lists[listName];

            SPWorkflowAssociation association = list.WorkflowAssociations.GetAssociationByName(workflowName,
                                                                                               CultureInfo.
                                                                                                   CurrentCulture);

            using (SPSite site = web.Site)
            {
                SPWorkflowManager manager = site.WorkflowManager;

                SPListItem item = list.Items[itemID];
                manager.StartWorkflow(item, association, "Some information here", true);
            }
        }

        /// <summary>
        /// 
        /// </summary>
        /// <param name="web"></param>
        /// <param name="workflowName"></param>
        /// <param name="listName"></param>
        /// <param name="newWorkflowHistoryListName"></param>
        public static void UpdateWorkflowHistoryList(SPWeb web, string workflowName, string listName,
                                                     string newWorkflowHistoryListName)
        {
            SPList list = web.Lists[listName];
            SPList workflowlist = web.Lists[newWorkflowHistoryListName];

            SPWorkflowAssociation association = list.WorkflowAssociations.GetAssociationByName(workflowName,
                                                                                               CultureInfo.
                                                                                                   CurrentCulture);
            association.SetHistoryList(workflowlist);
            list.UpdateWorkflowAssociation(association);
        }

        /// <summary>
        /// 
        /// </summary>
        /// <param name="web"></param>
        /// <param name="listName"></param>
        /// <param name="workflowName"></param>
        /// <param name="days"></param>
        public static void UpdateAutoCleanUp(SPWeb web, string listName, string workflowName, int days)
        {
            SPList list = web.Lists[listName];
            SPWorkflowAssociation association = list.WorkflowAssociations.GetAssociationByName(workflowName,
                                                                                               CultureInfo.
                                                                                                   CurrentCulture);

            association.AutoCleanupDays = days;
            list.UpdateWorkflowAssociation(association);
        }
    }
}